//
//  OPSearchTextView.m
//  OpenPractice
//
//  Created by op on 2021/10/21.
//  Copyright © 2021 tanyuehong. All rights reserved.
//

#import "OPSearchTextView.h"

@interface OPSearchTextView ()

@property (nonatomic, assign) BOOL curIndex;
@property (nonatomic, assign) BOOL loopShoudStop;
@property (nonatomic, strong) UILabel *currentShowLabel;
@property (nonatomic, strong) NSArray<NSString *> *texts;
@property (nonatomic, strong) NSMutableArray<UILabel *> *loopLabels;

@end


@implementation OPSearchTextView

- (instancetype)initWithFrame:(CGRect)frame texts:(NSArray<NSString *> *)texts {
    self = [super initWithFrame:frame];
    if (self) {
        NSAssert(texts.count > 0, @"滚动文案不能为空");
        _loopShoudStop = NO;
        _curIndex = 0;
        _loopLabels = [[NSMutableArray alloc] init];
        _texts = texts;
        _searchTextViewVisable = YES;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    self.userInteractionEnabled = NO;
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    firstLabel.textColor = [UIColor colorWithHex:0x626466];
    firstLabel.text = [_texts objectAtIndex:_curIndex];
    firstLabel.font = [UIFont systemFontOfSize:14.f weight:UIFontWeightRegular];
    [self addSubview:firstLabel];
    self.currentShowLabel = firstLabel;
    [_loopLabels addObject:firstLabel];
    
    UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.f, CGRectGetHeight(self.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
    secondLabel.textColor = [UIColor colorWithHex:0x626466];
    secondLabel.font = [UIFont systemFontOfSize:14.f weight:UIFontWeightRegular];
    [self addSubview:secondLabel];
    [_loopLabels addObject:secondLabel];
    @op_weakify(self);
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        @op_strongify(self);
        [self startLoop];
    });
}

- (void)startLoop {
    if (_texts.count <= 1) {
        self.loopShoudStop = YES;
        return;
    }
    self.loopShoudStop = NO;
    if (_curIndex >= _texts.count-1) {
        _curIndex = 0;
    }else {
        _curIndex++;
    }
    [UIView animateWithDuration:0.28f delay:3.52f options:UIViewAnimationOptionCurveEaseOut animations:^{
        if (self.currentShowLabel) {
            self.currentShowLabel.frame = CGRectMake(CGRectGetMinX(self.currentShowLabel.frame), -CGRectGetHeight(self.currentShowLabel.frame), CGRectGetWidth(self.currentShowLabel.frame), CGRectGetHeight(self.currentShowLabel.frame));
        }
    } completion:^(BOOL finished) {
        if (finished) {
            if (self.currentShowLabel) {
                self.currentShowLabel.frame = CGRectMake(CGRectGetMinX(self.currentShowLabel.frame), CGRectGetHeight(self.frame), CGRectGetWidth(self.currentShowLabel.frame), CGRectGetHeight(self.currentShowLabel.frame));
            }
            [self->_loopLabels exchangeObjectAtIndex:0 withObjectAtIndex:1];
            self.currentShowLabel = self->_loopLabels.firstObject;
            self.currentShowLabel.text = [self->_texts objectAtIndex:self->_curIndex];
            [UIView animateWithDuration:0.28f delay:0.08f options:UIViewAnimationOptionCurveEaseIn animations:^{
                if (self.currentShowLabel) {
                    self.currentShowLabel.frame = CGRectMake(CGRectGetMinX(self.currentShowLabel.frame), 0, CGRectGetWidth(self.currentShowLabel.frame), CGRectGetHeight(self.currentShowLabel.frame));
                }
            } completion:^(BOOL finished) {
                if (finished) {
                    [self startLoop];
                }else {
                    self.loopShoudStop = YES;
                }
            }];
            
            [UIView animateWithDuration:0.2f delay:0.16f options:UIViewAnimationOptionCurveEaseOut animations:^{
                if (self.currentShowLabel) {
                    self.currentShowLabel.alpha = 1.f;
                }
            } completion:nil];
        }else {
            self.loopShoudStop = YES;
        }
    }];
    
    [UIView animateWithDuration:0.2f delay:3.52f options:UIViewAnimationOptionCurveEaseOut animations:^{
        if (self.currentShowLabel) {
            self.currentShowLabel.alpha = 0.f;
        }
    } completion:nil];
}

- (void)resetLoopTexts:(NSArray<NSString *> *)texts {
    _texts = texts;
    _curIndex = 0;
    if (self.loopShoudStop) {
        [self startLoop];
    }
}

- (NSString *)curShowText {
    return self.currentShowLabel.text;
}

@end
